package com.dstz.bpm.plugin.usercalc.user.executer;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.engine.plugin.def.BpmUserCalcPluginDef;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.def.BpmProcessDef;
import com.dstz.bpm.api.model.inst.IBpmInstance;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.engine.model.BpmIdentity;
import com.dstz.bpm.engine.plugin.runtime.abstact.AbstractUserCalcPlugin;
import com.dstz.bpm.engine.plugin.session.BpmUserCalcPluginSession;
import com.dstz.bpm.plugin.usercalc.user.def.UserPluginDef;
import com.dstz.org.api.model.IUser;
import com.dstz.org.api.service.UserService;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class UserPluginExecutor extends AbstractUserCalcPlugin<UserPluginDef> {
   @Resource
   BpmTaskOpinionManager y;
   @Resource
   UserService I;
   @Resource
   BpmProcessDefService J;

   @Override
   public List<SysIdentity> queryByPluginDef(BpmUserCalcPluginSession pluginSession, UserPluginDef def) {
      List<SysIdentity> list = new ArrayList();
      String source = def.getSource();
      if ("start".equals(source)) {
         BpmProcessDef processDef = this.J.getBpmProcessDef(pluginSession.getBpmTask().getDefId());
         String startNodeId = processDef.getStartEvent().getNodeId();
         List<BpmTaskOpinion> opinions = this.y.getByInstAndNode(pluginSession.getBpmTask().getInstId(), startNodeId);
         BpmIdentity bpmIdentity;
         if (CollectionUtil.isEmpty(opinions)) {
            IBpmInstance instance = pluginSession.getBpmInstance();
            if (StringUtil.isEmpty(instance.getCreateBy())) {
               throw new BusinessException("流程数据异常，无法获取发起人！", BpmStatusCode.USER_CALC_ERROR);
            }

            bpmIdentity = new BpmIdentity(instance.getCreateBy(), instance.getCreator(), "user");
            list.add(bpmIdentity);
         } else {
            BpmTaskOpinion firstNode = (BpmTaskOpinion)opinions.get(0);
            bpmIdentity = new BpmIdentity(firstNode.getApprover(), firstNode.getApproverName(), "user");
            list.add(bpmIdentity);
         }
      }

      if ("currentUser".equals(source)) {
         SysIdentity bpmIdentity = new BpmIdentity(ContextUtil.getCurrentUser());
         list.add(bpmIdentity);
      } else if ("spec".equals(source)) {
         String userKeys = def.getAccount();
         String[] aryAccount = userKeys.split(",");
         String[] var16 = aryAccount;
         int var18 = aryAccount.length;

         for(int var19 = 0; var19 < var18; ++var19) {
            String account = var16[var19];
            IUser user = this.I.getUserByAccount(account);
            if (user == null) {
               throw new BusinessException(account + "用户丢失", BpmStatusCode.USER_CALC_ERROR);
            }

            SysIdentity bpmIdentity = new BpmIdentity(user);
            list.add(bpmIdentity);
         }
      }

      return list;
   }
}
